<template>
	
	<view class="">
		<product-detail ref="mychild1" :groupType='groupType' :carriagePrice='carriagePrice' :carriageLimit='carriageLimit' :productId='productId' v-if="suitFlg==1" @popupShowFun='popupShowFun'></product-detail>
		<suit-detail ref="mychild" :carriagePrice='carriagePrice' :carriageLimit='carriageLimit' :productId='suitId' v-if="suitFlg==0" @popupShowFun='popupShowFun'></suit-detail>
		<!-- <suit-detail :productId='productId' :showproduct='showproduct'></suit-detail> -->
		<!-- 规格面板 -->
		<sku :popupShow.sync="popupShow" :preSaleTimeType='preSaleTimeType' :presaleNum='presaleNum' :preSaleType='preSaleType' :bargainId="bargainId" :productLevelPrice='productLevelPrice' :preSaleFlag='preSaleFlag' :cartNum='carCount' :depositPrice='depositPrice' :productPrice='productDiscountPrice' :type='skuType' ref="skuPopup" :imgNoUrl='productMainImageUrl' :specificationId='specificationId' :skuList="skuList" @closeFun='popupShow=false' @addToCart='addToCart' @chooseAttr='chooseAttr' ></sku>
		<loading v-if="isLoading"></loading>
		<tui-modal></tui-modal>
	</view>
</template>

<script>
	// import tuiButton from "@/components/button/button"
	import mixin from "@/utils/mixin.js"
	import sku from "@/packageA/components/sku/sku.vue"
	import productDetail from "./productDetail.vue"
	import suitDetail from "./suitDetail.vue"
	// import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";

	export default {
		mixins: [mixin],
		components: {
			// uParse,
			// tuiButton,
			sku,
			productDetail,
			suitDetail
		},
		data() {
			return {
				suitId:'',
				productId:'',
				suitFlg:-1,
				popupShow: false,
				carCount:0,// 购物车数量
				isLoading:false, // 显示loading
				productDiscountPrice:0,// sku商品价格
				skuType:'',  // sku类型  加入购物车  立即后买  立即预购
				productMainImageUrl:'',// 商品主图
				skuList:[],//shu数据
				onloadsuitFlg:false,
				depositPrice:"", // 定金
				preSaleFlag:'', // 判断是否为预售商品
				goodData:'',
				showproduct:true,
				carriagePrice:0,
				carriageLimit:0,
				productLevelPrice:0,
				groupType:0,
				bargainId:'',
				presaleNum:0, // 预售起拍数量
				preSaleType:0, // 预售付定金方式
				preSaleTimeType:'',
				specificationId:''
			}
		},
		onShow() {
			// this.queryGoodsDetail(this.productId);
			// this.getBuyItem()
		},
		onReady() {
			// #ifdef APP-PLUS
			const subNvue = uni.getSubNVueById('swiper');//初始化subnvue对象 'swiper'为pages.josn中注册时的唯一标识
			subNvue.show();//显示subnvue;更多方法请·查看官方文档
			// #endif
		},
		onLoad(options) {
			// debugger
			console.log(options)
			if (!this.$util.checkTokenAndInviterId(options)) {
			    // 检查token和邀请人信息，无token退出方法
			    return;
			   }
			let token =this.$util.getToken()
			if(token){
				this.carriageSettingFun()
				if(options.productId){
					this.suitFlg=1 //是显示商品详情还是显示套装详情
					this.onloadsuitFlg=false // 是不是从套装商品进来的
					this.productId=options.productId
					
				}else if(options.suitId){
					this.suitFlg=0				
					this.onloadsuitFlg=true
					this.suitId=options.suitId
				}
				if(options.specificationId){
					this.specificationId=options.specificationId
				}else{
					this.specificationId=''
				}
				if(options.groupType){
					this.groupType=options.groupType
					this.bargainId = options.bargainId
				}
				console.log(this.suitFlg)
			}else{
				// #ifdef H5
				uni.setStorageSync('loginSuccessUrl', window.location.href);
				// #endif
				uni.navigateTo({
					url:'/pages/home/login'
				})
			}
			
		},
		computed: {
		},
		onShareAppMessage(res) {
			let inviterId=uni.getStorageSync('shareAgencyId')
			if(this.suitFlg===0){
				return {
					title: this.goodData.goodsName,
					path: `packageA/pages/goodsDetails/goodsDetails?suitId=${this.suitId}&inviterId=${inviterId}` //(可携带参数)
				}
			}else{
				return {
					title: this.goodData.goodsName,
					path: `packageA/pages/goodsDetails/goodsDetails?productId=${this.productId}&inviterId=${inviterId}` //(可携带参数)
				}
			}
			
		},
		beforeDestroy() {
			// #ifdef H5
			this.$wechat.cleanShare();
			// #endif
		},
		watch: {
			popupShow(newValue) {
				// #ifdef APP-PLUS
				uni.$emit('skuShow', {
					skuShow: newValue,
				})
				// #endif
			},
		},
		methods: {
			// 获取运费
			carriageSettingFun(){
				this.$http.carriageSetting().then((res) => {
					if (res.status === 0) {
						this.carriagePrice=res.data.carriagePrice
						this.carriageLimit=res.data.carriageLimit
					}
				})
			},
			// 点击了商品的套装规格
			chooseAttr(data){
				console.log(data)
				if(data.suitId){
					this.suitId=data.suitId
					this.suitFlg=0
				}else{
					this.suitFlg=1
				}
			},
			// 显示规格
			popupShowFun(data){
				this.goodData=data
				console.log(this.goodData)
				if(this.goodData.suitId){
					if(this.onloadsuitFlg){
						this.skuList= [{
							suitId:this.goodData.suitId,
							specificationId:this.goodData.suitId,
							isShoppingCart:this.goodData.isShoppingCart,
							specificationName:this.goodData.suitName,
							productThumbnailImageUrl:this.goodData.productThumbnailImageUrl,
							activityType:this.goodData.activeType, // 活动类型
							activityId:this.goodData.activeId // 活动Id
						}]
						this.productDiscountPrice=this.goodData.discountPrice
					}
					this.productMainImageUrl=this.goodData.productThumbnailImageUrl
				}else{
					let skuData=[]
					console.log(skuData)
					
					skuData= this.goodData.productSpecificationData
					if(data.skuType!=='group'){
						if(this.goodData.suitData){
							this.goodData.suitData.forEach((item)=>{
								skuData.push({
									suitId:item.suitId,
									discountPrice:item.discountPrice,
									isShoppingCart:item.isShoppingCart,
									specificationId:item.suitId,
									specificationName:item.suitName,
									productThumbnailImageUrl:item.productThumbnailImageUrl,
									virtualInventory:item.virtualInventory,
									activityType:item.activeType, // 活动类型
									activityId:item.activeId // 活动Id
								})
							})
						}
						
					}
					if(data.skuType==='group'){
						this.productLevelPrice=this.goodData.productLevelPriceData.productLevelPrice
					}
					this.isShoppingCart=1
					this.preSaleFlag=this.goodData.preSaleFlag
					if(this.goodData.preSaleFlag!==1){
						this.productDiscountPrice=this.goodData.productDiscountPrice
					}else{
						if(this.goodData.preSaleDateType==2){
							if(this.tab(this.goodData.preSaleDateFixedStartTime)){
								this.preSaleTimeType='1'
							}
							if(this.normalStatus(this.goodData.preSaleDateFixedEndTime)){
								this.preSaleTimeType='2'
							}
						}
						if(this.goodData.preSaleType===1){//全款预售
								this.productDiscountPrice=this.goodData.productDiscountPrice
								this.depositPrice=this.goodData.productDiscountPrice
						}else if(this.goodData.preSaleType===2){ //比例预售
								this.productDiscountPrice=this.goodData.productDiscountPrice
								// this.depositPrice=this.goodData.productDiscountPrice*this.goodData.depositRate*0.01
								if(this.goodData.roundNumberFlag===1){
									this.depositPrice =Math.ceil(this.goodData.productDiscountPrice * this.goodData.depositRate * 0.01);
								}else{
									this.depositPrice =(this.goodData.productDiscountPrice * this.goodData.depositRate * 0.01).toFixed(2);
								}
						}else if(this.goodData.preSaleType===3){ //固定预售
								this.productDiscountPrice=this.goodData.productDiscountPrice
								this.depositPrice=this.goodData.deposit
						}
						this.presaleNum=this.goodData.startBuyCount
						if(this.presaleNum){
							this.depositPrice=this.depositPrice*this.presaleNum
							this.productDiscountPrice=this.productDiscountPrice*this.presaleNum
						}
						this.preSaleType=this.goodData.preSaleType
					}
					console.log(this.productDiscountPrice)
					this.skuList=skuData
					this.productMainImageUrl=this.goodData.productThumbnailImageHdUrl
				}
				this.showPopup(data.skuType)
			},
			// 获取新的购物车数量
			addToCart(data){
				if(data.suitId){
					this.$refs.mychild.getBuyItem()
				}else{
					this.$refs.mychild1.getBuyItem()
				}
				this.popupShow=false
			},
			showPopup(type) {
				this.skuType= type
				this.popupShow = true;
			},
			tab(date) {
				if (date) {
					var endtime = date.split(' ');
					// console.log(endtime)
					if (endtime[1] === '24:00:00') {
						endtime[1] = '23:59:59'
					}
					date = endtime.join(' ')
					let datatime = date.replace(/\-/g, "/")
					var oDate1 = new Date(datatime);
					var oDate2 = new Date();
					if (oDate1.getTime() > oDate2.getTime()) {
						return true
					} else {
						return false
					}
				} else {
					return false
				}
			
			},
			
			//正常商品 判断处于什么是否上下架状态
			normalStatus(date) {
				if (date) {
					var endtime = date.split(' ');
					// console.log(endtime)
					if (endtime[1] === '24:00:00') {
						endtime[1] = '23:59:59'
					}
					var dateEnd = endtime.join(' ')
					let datatime = dateEnd.replace(/\-/g, "/")
					var oDate1 = new Date(datatime);
					var oDate2 = new Date();
					if (oDate1.getTime() < oDate2.getTime()) {
						return true
					} else {
						return false
					}
				} else {
					return false
				}
			
			},
			// queryGoodsDetail(id) {
			// 	this.banner = []
			// 	this.isLoading = true
			// 	this.$http.productDetail(id).then((res) => {
			// 		this.isLoading = false
			// 		if (res.status === 0) {
			// 			productData=res.data
			// 		}
			// 	})
			// },
		},

	}
</script>

<style lang="scss">
	.backgroundClass {
		background-color: #f2f2f2;
	}
	
	.container {
		padding-bottom: 100rpx;
	}
</style>
